package com.rhzy.wnhc.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rhzy.wnhc.entity.TPerson;
import com.rhzy.wnhc.service.TPersonService;
import com.rhzy.wnhc.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "小程序端接口-数据统计")
@RestController
@RequestMapping("/statis")
public class StatisticsController {

    @Autowired
    TPersonService personService;
    @ApiOperation(value = "授权码认证(本村劳动力：totalCount；就业劳动力：jobCount；返乡劳动力：backHomeCount；需乘车数：outTrainCount)", tags = {"小程序端接口-数据统计",})
    @RequestMapping(value = "/getByAreaCode/{areacode}", method = RequestMethod.GET)
    @Transactional
    public AjaxResult addWishJob(@PathVariable String areacode){
        try{

            if(StringUtils.isEmpty(areacode)){
                return AjaxResult.addError("地址码为空");
            }
            int totalCount=personService.count(new LambdaQueryWrapper<TPerson>().eq(TPerson::getAddressCode,areacode));
            int jobCount=personService.count(new LambdaQueryWrapper<TPerson>().eq(TPerson::getAddressCode,areacode));
            int backHomeCount=personService.count(new LambdaQueryWrapper<TPerson>().eq(TPerson::getAddressCode,areacode).eq(TPerson::getBackHomeStatus,1));
            int outTrainCount=personService.count(new LambdaQueryWrapper<TPerson>().eq(TPerson::getAddressCode,areacode).eq(TPerson::getOutTarinStatus,1));
            Map map=new HashMap();
            map.put("totalCount"," ");
            map.put("jobCount",jobCount);
            map.put("backHomeCount",backHomeCount);
            map.put("outTrainCount",outTrainCount);
            return AjaxResult.addSuccess(map);

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.addError("异常："+e.getMessage());
        }
    }
    @ApiOperation(value = "分类劳动力分页查询", tags = {"小程序端接口-数据统计",})
    @RequestMapping(value = "/pageList/{areacode}/{type}/{pageIndex}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="areacode",value="地址码",dataType="string",required = true),
            @ApiImplicitParam(name="type",value="查询类别(1:就业劳动力 2：返乡劳动力 3：需乘车人)",required = true),
            @ApiImplicitParam(name="pageIndex",value="当前页",required = true)
    })
    public AjaxResult pageList(@PathVariable String areacode,@PathVariable Integer type,@PathVariable Integer pageIndex)
    {
        if(StringUtils.isEmpty(areacode)||type==null){
            return AjaxResult.addError("参数不全");
        }
        LambdaQueryWrapper<TPerson> queryWrapper =  new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(TPerson::getId);
        switch (type){
            case 1:
               queryWrapper.eq(TPerson::getAddressCode,areacode);
               break;
            case 2:
               queryWrapper.eq(TPerson::getAddressCode,areacode).eq(TPerson::getBackHomeStatus,1);
               break;
            case 3:
                 queryWrapper.eq(TPerson::getAddressCode,areacode).eq(TPerson::getOutTarinStatus,1);
                 break;
        }
        Page<TPerson> page = new Page<TPerson>(pageIndex,10);  // 查询第1页，每页返回10条
        IPage<TPerson> iPage = personService.page(page,queryWrapper);
        return AjaxResult.addSuccess(iPage,"查询成功！");
    }
}
